﻿using LiteBlog.Common.Entities;
using LiteBlog.Components.Contracts;
using LiteBlog.Components.Enums;
using LiteBlog.Controllers.Contracts;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiteBlog.Controllers.Tests
{
    [TestClass]
    public class CompControllerTests<T, U, V, W> : ControllerTests<W>   where T: new()
                                                                        where V: class, IComp<T,U> 
                                                                        where W: ICompController<T,U>
    {
        protected W _compController;

        [TestInitialize]
        public override void InitTest()
        {
            base.InitTest();
            _compController = (W)_controller;
        }

        [TestMethod]
        public void ComponentIsNotNull()
        {
            Assert.IsNotNull(_compController);
        }

        [TestMethod]
        public void ManageGetsCollection()
        {
            _compController.Manage();
            var mock = Mock.Get<V>(_compController.GetComponent<V>());
            mock.Verify(c => c.Get());
        }

        [TestMethod]
        public virtual void CreateWorks()
        {
            var model = new T();
            var mock = Mock.Get<V>(_compController.GetComponent<V>());
            mock.Setup(c => c.Create(It.IsAny<T>())).Returns(MethodResult.CreateSuccess);
            _compController.Create(model);
            mock.Verify(c => c.Create(model));
        }

        [TestMethod]
        public virtual void UpdateWorks()
        {
            string olid = "test";
            var model = new T();
            var mock = Mock.Get<V>(_compController.GetComponent<V>());
            mock.Setup(c => c.Update(It.IsAny<string>(), It.IsAny<T>())).Returns(MethodResult.UpdateSuccess);
            _compController.Update(olid, model);
            mock.Verify(c => c.Update(olid, model));
        }

        [TestMethod]
        public virtual void DeleteGetsItem()
        {
            var mock = Mock.Get<V>(_compController.GetComponent<V>());
            mock.Setup(c => c.Get(It.IsAny<U>())).Returns(new T());
            mock.Setup(c => c.Delete(It.IsAny<U>())).Returns(MethodResult.DeleteSuccess);
            var id = It.IsAny<U>();
            _compController.Delete(id);
            mock.Verify(c => c.Get(id));
        }

        [TestMethod]
        public virtual void DeleteWorks()
        {
            var mock = Mock.Get<V>(_compController.GetComponent<V>());
            mock.Setup(c => c.Get(It.IsAny<U>())).Returns(new T());
            mock.Setup(c => c.Delete(It.IsAny<U>())).Returns(MethodResult.DeleteSuccess);
            var id = It.IsAny<U>();
            _compController.Delete(id);
            mock.Verify(c => c.Delete(id));
        }
    }
}
